package com.fairytask.managers
{
	import com.fairytask.data.Modulator;
	import com.fairytask.engine.GameStage;
	import com.fairytask.engine.components.ComponentCreater;
	import com.fairytask.engine.define.UIDefine;
	import com.fairytask.engine.utils.IO;
	import com.fairytask.event.ChatEvent;
	import com.fairytask.resource.StaticIni;
	import com.fairytask.ui.base.BaseUI;
	import com.fairytask.ui.base.FrameUI;
	
	import flash.display.DisplayObjectContainer;
	import flash.display.Sprite;
	import flash.events.Event;
	import flash.utils.getDefinitionByName;
	
	public class ViewManage
	{
		private static 	var _self			:ViewManage;
		//默认打开界面在最大数量 
		public static const  _uiNo			:int = 30;
		
		private 			var _maskUI			:Sprite;
		
		private 			var _custMaskUI		:Sprite;
		
		private				var _maskEnable		:Boolean	=	true;
		
		public  			var uiArray		:Array  	= [];
		private var _model		:Modulator;
		
		public function ViewManage()
		{
			_maskUI	=	new Sprite();
			_maskUI.graphics.beginFill(0x333333,0.2);
			_maskUI.graphics.drawRect(-220,-150,1440,900);
			_maskUI.graphics.endFill();
			
			_model	=	Modulator.instance;
		}
		
		public static function getInstance():ViewManage
		{
			if(_self == null)
			{
				_self = new ViewManage();
			}
			return _self;
		}
		
		public function CreateUI():void 
		{
			
		}
		
		/**
		 * 水平广播滚动条相关显示事件 
		 * @param evt
		 * 
		 */		
		private function onBroadCast(evt:ChatEvent):void
		{
			if(evt.type == ChatEvent.BROADCAST_MESSAGE)
			{
			}
			else if(evt.type == ChatEvent.BROADCAST_SYSTEM_MESSAGE)
			{
			}
		}
		
		/**
		 * 显示UI 
		 * @param evt
		 * 
		 */
		private function onShowUI(evt:Event):void
		{
			switch(evt.type)
			{
				
			}
		}
		
		private function getArrayByStr(val:String):*
		{
			var pattern:RegExp = /^[$].*[$]$/;
			
			if(val.match(pattern))
			{
				if(val.length > 2)
				{
					var	strSub:String	=	val.substr(1, val.length - 2);
					return	strSub.split(";");	
				}
				else
				{
					return	[];
				}
			}
			return null;		
		}
		
		
		private function getLangStr(val:String):String
		{
			var pattern:RegExp = /^[##].+[##]$/;
			
			if(	val.match(pattern))
			{						
				var strSub:String	=	val.substr(2, val.length - 4);
				return StaticIni.getSwfLangStr(strSub);							
			}
			return val;
		}
		
		private function checkArrLang(arr:Array):Array
		{
			if(arr == null || arr.length == 0)
			{
				return null;
			}
			var tSize:uint	=	arr.length;
			for(var i:uint = 0 ; i < tSize ; i++)
			{
				arr[i]	=	getLangStr(arr[i])
			}
			return arr;
		}
		private function CreateUIControl(controls:Array,ui:BaseUI,className:String):void
		{
			var parentSpace	:DisplayObjectContainer;
			
			var tSize		:uint	=	controls.length;
			
			//组件变量
			var controlsVar	:String;
			//组件类型
			var controlType	:String;
			//组件参数
			var controlSet	:Array;
			
			//父对象
			var controlParent	:Object;
			
			for(var i:uint = 0 ; i < tSize ; i ++)
			{
				controlsVar	=	controls[i];
				
				controlSet	=	StaticIni.getIniVar(className+"_"+controlsVar,"create").split(",");
				
				controlType	=	controlSet.shift();
				
				for	(var iSet:int	=	0;	iSet < controlSet.length; iSet++)
				{
					var	 strArr:String	=	controlSet[iSet];
					
					var	 obj:*			=	getArrayByStr(strArr);
					if( null == obj)
					{
						if(controlSet[iSet] is Boolean)
						{
							controlSet[iSet]	=	Boolean(strArr);
						}
						else if(controlSet[iSet] is String)
						{
							controlSet[iSet]	= getLangStr(strArr);
						}
						else if(controlSet[iSet] is int)
						{
							controlSet[iSet]	= int(strArr)
						}
						else if(controlSet[iSet] is Number)
						{
							controlSet[iSet]	= Number(strArr);
						}
					}
					else
					{
						if(obj is Array)
						{
							controlSet[iSet] = checkArrLang(obj);
						}
						else
						{
							controlSet[iSet] = getLangStr(strArr);
						}	
					}
				}
				
				ComponentCreater.getInstance().CreateControl(controlType,controlSet,ui,controlsVar);
				
				controlParent			=	StaticIni.getIniVar(className+"_"+controlsVar,"parent");
				
				if(null != controlParent && controlParent != "")
				{
					if(controlParent == "this")
					{
						//当配置成“this”时，显示对象为界面类本身
						parentSpace	=	ui	as DisplayObjectContainer;
					}
					else
					{
						parentSpace	=	ui[controlParent]	as DisplayObjectContainer;
					}
					
					switch(controlType)
					{
						default:
							parentSpace.addChild(ui[controlsVar]);
							break;	
					}
				}
				var	contentArr:Object	=	StaticIni.getIniObj(className+"_"+controlsVar);
				
				for(var p:String in contentArr)
				{
					if("parent" != p && "create" != p)
					{
						strArr	=	contentArr[p];
						
						obj		=	getArrayByStr(strArr);
						
						if(obj != null)
						{
							if(obj is Array)
							{
								contentArr[p] = checkArrLang(obj);
							}
							else
							{
								contentArr[p] = getLangStr(strArr);
							}
						}
						
						if(ui[controlsVar][p] is Boolean)
						{
							ui[controlsVar][p]	=	Boolean(int(contentArr[p]));
						}
						else if(ui[controlsVar][p] is String)
						{
							ui[controlsVar][p]	= getLangStr(contentArr[p]);
						}
						else if(ui[controlsVar][p] is int)
						{
							ui[controlsVar][p]	= int(contentArr[p])
						}
						else if(ui[controlsVar][p] is Number)
						{
							ui[controlsVar][p]	= Number(contentArr[p]);
						}
						else if(ui[controlsVar][p] == null)
						{
							ui[controlsVar][p]	= contentArr[p];
						}
					}
				}					
			}
		}
		
		/**
		 * 初始化界面显示 
		 * @param ui
		 * @param className
		 * 
		 */
		public function initUI(ui:BaseUI,className:String):void
		{
			if(!ui.isInit)
			{
				var	arrObj:Object	=	StaticIni.getIniObj(className);
				for(var p:String in arrObj)
				{
					if("controls"	==	p)
					{
						var controls:Array	=	arrObj[p].split(",");
						CreateUIControl(controls,ui,className);
					}
					else
					{
						ui[p]	=	arrObj[p];
					}
				}			
				ui.initGUI();
			}
		}
		/**
		 * 屏蔽所有已打开的UI界面(排除不屏蔽的) 
		 * 
		 */		
		public function hideOpenedUI(notHideArray:Array):void
		{
			while (uiArray.length > 0)
			{
				var curUI		:FrameUI = uiArray.shift() as FrameUI;
				var hide		:Boolean = true;
				for(var i:int=0;i<notHideArray.length;i++)
				{
					if(curUI == notHideArray[i])
					{
						notHideArray.splice(i,1);
						hide = false;
						break;
					}	
				}
				
				if(hide&&curUI.popupUI)//屏蔽
				{
					curUI.hide();	
				}
				
			}
		}
		
		/**
		 * 检测要关闭的UI 
		 * 
		 */
		public static function checkOpenUI():void
		{
			
		}
		
		public static function set mask(val:Boolean):void
		{
			
		}
		
		public function get maskEnable():Boolean
		{
			return _maskEnable;
		}
		
		public static function set maskEnable(value:Boolean):void
		{
			
			if(value)
			{
				GameStage.uiSpace.addChildAt(_self._maskUI,0);
			}
			else
			{
				if(_self._maskUI.parent!=null)
				{
					_self._maskUI.parent.removeChild(_self._maskUI);
				}
			}
		}
		
		/**
		 * 根据UI ID取得实例  
		 * @param pNum
		 * @return 
		 * 
		 */
		public static function retrieveUIByID(pNum:int):BaseUI
		{
			IO.traceLog("取得的类名是:::::",UIDefine.getUIById(pNum));
			var tmpClass	:Class	=	getDefinitionByName(UIDefine.getUIById(pNum)) as Class;
			return tmpClass.getInstance() as BaseUI;
		}
	}
}
